package com.ys.controller.web.mall;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bo.vo.W;
import com.ys.entity.mall.TmCate;
import com.ys.service.sys.CommonApiService;

import lombok.AllArgsConstructor;
import lombok.Data;

@Controller
@RequestMapping("/web/mall/tmCate")
public class WebTmCateController {
	
	public static Long MENU_ID = 10156L;
	
	@Autowired
	private CommonApiService<TmCate> commonService;
	
	/*
	 * 新增
	 */
	@RequiresPermissions("tmCate:add")
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response){
		
		TmCate entity = new TmCate();
		
		try {
			return commonService.toAdd(model, entity, MENU_ID, null, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	
	/*
	 * 编辑
	 */
	@RequiresPermissions("tmCate:edit")
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id){

		try {
			return commonService.toEdit(model, id, new TmCate(), MENU_ID, null, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	
	/*
	 * 调整顺序
	 */
	@GetMapping("/toResort")
	public String toResort(Model model, HttpServletRequest request, HttpServletResponse response){
		
		List<TmCate> listEntity = new TmCate().setOrderby("A.SortNo asc").queryList();;
		
		List<ToResortDto.RowDto> rows = new ArrayList<ToResortDto.RowDto>();
		for(TmCate entity : listEntity) {
			Long guid = entity.getId();
			String name = entity.getName();
			rows.add(new ToResortDto.RowDto(guid, name));
		}
		
		ToResortDto dto = new ToResortDto(rows);
		model.addAttribute("dto", dto);
		return "mall/tmCate/toResort";
	}
	@Data
	@AllArgsConstructor
	public static class ToResortDto{
		private List<RowDto> rows; // 数据行
		
		@Data
		@AllArgsConstructor
		public static class RowDto {
			private Long id; // 字段ID
			private String name; // 字段名称
		}
	}
}
